package com.project.biz.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.biz.CmsProductCategoryBiz;
import com.project.entity.CmsProductCategory;
import com.project.mapper.CmsProductCategoryMapper;
import com.project.pojo.model.AddOrEditCategoryDTO;
import com.project.pojo.model.CategoryModel;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品分类表 服务实现类
 * </p>
 *
 * @author wangzhiqiang
 * @since 2019-03-06
 */
@Service
public class CmsProductCategoryBizImpl extends ServiceImpl<CmsProductCategoryMapper, CmsProductCategory> implements CmsProductCategoryBiz {

    /**
     * 查询商品分类列表数据
     * @param categoryModel
     * @return
     */
    public List<CmsProductCategory> getCategory(CategoryModel categoryModel){
        return this.baseMapper.getCategory(categoryModel);
    }

    /**
     * 新增修改商品类目
     * @param addOrEditCategoryDTO
     * @return
     */
    public int addOrEditEategory(AddOrEditCategoryDTO addOrEditCategoryDTO, Long baseUserId){
        CmsProductCategory okbuydGoodsCategory = null;
        int result = 0;
        if (addOrEditCategoryDTO.getCategoryId() != null){//修改
            okbuydGoodsCategory = this.getById(addOrEditCategoryDTO.getCategoryId());
            if (null != okbuydGoodsCategory){
                okbuydGoodsCategory.setCategoryName(addOrEditCategoryDTO.getCategoryName());
                okbuydGoodsCategory.setCategoryImg(addOrEditCategoryDTO.getCategoryImg());
                okbuydGoodsCategory.setParentId(addOrEditCategoryDTO.getParentId());
                okbuydGoodsCategory.setUpdateTime(new Date());
                okbuydGoodsCategory.setUpdateUser(baseUserId);
                result = this.baseMapper.updateById(okbuydGoodsCategory);
            }
        }else {
            okbuydGoodsCategory = new CmsProductCategory();
            okbuydGoodsCategory.setCategoryName(addOrEditCategoryDTO.getCategoryName());
            okbuydGoodsCategory.setCategoryImg(addOrEditCategoryDTO.getCategoryImg());
            okbuydGoodsCategory.setParentId(addOrEditCategoryDTO.getParentId());
            okbuydGoodsCategory.setCreateUser(baseUserId);
            okbuydGoodsCategory.setCreateTime(new Date());
            result = this.baseMapper.insert(okbuydGoodsCategory);
        }
        return result;
    }

}
